package gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import evolutionaryProblems.Phenotype;


public class PhenotypeList extends JPanel{
	
	private static final long serialVersionUID = 1L;
	@SuppressWarnings("rawtypes")
	private GameModel gameModel; 
	private JList<Phenotype> list; 
	private Dimension dimention = new Dimension(125, 500);
	
	
	public PhenotypeList(@SuppressWarnings("rawtypes") GameModel gameModel, ListModel<Phenotype> listModel){
		super(new GridBagLayout());
		this.gameModel = gameModel; 
		list = new JList<Phenotype>(listModel); 
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
		list.addListSelectionListener(new ListListner()); 
		initGUI(); 
	}
	
	private void initGUI() {
		GridBagConstraints constraints = new GridBagConstraints(); 
		constraints.gridx = 0; 
		constraints.gridy = 0; 
//		list.setPreferredSize(new Dimension(dimention.width - 10, dimention.height - 10)); 
		JScrollPane scrollPane = new JScrollPane(list);
		scrollPane.setPreferredSize(dimention); 
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		this.add(scrollPane, constraints); 
		this.setPreferredSize(dimention);
	}

	private class ListListner implements ListSelectionListener{

		@Override
		public void valueChanged(ListSelectionEvent e) {
			if (e.getValueIsAdjusting()) {
				return;
			}
			gameModel.runSelectedPhenotype(list.getSelectedIndex()); 
		}

	}
	
}
